import { action, observable } from 'mobx';
import { listDeptByParentId, saveDept, listDeptType, deleteDept, listDept, listOrg } from '../service';
import loading from '../../../components/Loading';
import { message } from 'antd';
import  '../../../components/ChangeTheme';

class AppStore {

  // 机构抽屉参数
  @observable deptDrawerData = {
    loading: false,
    visible: false,
    editData: {},
  };

  // 部门列表参数
  @observable deptListData = {
    dataSource: [],
    loading: false,
    expandedKeys: [],
  };

  // 机构类型字典
  @observable deptType = [];

  @observable orgListData = {
    visible: false,
    dataSource: [],
    loading: false,
  };


  /**
   * 析构函数
   */
  constructor() {
    this.initData();
  }

  /**
   * 初始化页面，加载数据
   * @returns {Promise<void>}
   */
  async initData() {
    this.deptType = await listDeptType();
  }

  @action
  async orgOnload(firstRow) {
    this.deptListData.dataSource = await loading(listDept, this.deptListData)(firstRow.id);
  }

  @action
  async selectOrg(key) {
    const list = await loading(listDept, this.deptListData)(key);
    this.deptListData.dataSource = list;
  }

  /**
   * 展开列表
   * @param expanded 是否展开
   * @param record 展开的行
   */
  @action
  async expandRow(expanded, record) {
    if (record.children && record.children.length == 0) {
      record.children = await loading(listDeptByParentId, this.deptListData)(record.id);
    }
    let pos = -1;
    this.deptListData.expandedKeys.map(((value, index) => {
      if (value == record.id) pos = index;
    }));
    let expandedKeys = this.deptListData.expandedKeys;
    expanded ? expandedKeys.push(record.id) : expandedKeys.splice(pos, 1);

    this.deptListData = { ...this.deptListData, expandedKeys: expandedKeys };
  }

  /**
   * 新增或者修改组织机构
   * @param record 组织机构
   */
  async showDeptDrawer(record) {
    this.deptDrawerData = { ...this.deptDrawerData, editData: record, visible: true };
  }

  /**
   * 关闭组织机构抽屉
   */
  @action
  closeDeptDrawer() {
    this.deptDrawerData = { ...this.deptDrawerData, visible: false };
  };

  /**
   * 保存机构数据，并关闭对话框
   * @param dept
   * @returns {Promise<void>}
   */
  @action
  async saveDept(dept) {
    let resultDept = await loading(saveDept, this.deptDrawerData)(dept);

    if (dept.parentId == null) {
      let deptList = await loading(listDept, this.deptListData)(dept.orgId);
      this.deptListData = { ...this.deptListData, dataSource: deptList };
    } else {
      let findedDept = this.findDept(this.deptListData.dataSource, dept.parentId);
      let childrenDept = await loading(listDeptByParentId, this.deptListData)(dept.parentId);
      findedDept.children = childrenDept;
    }
    this.deptListData = { ...this.deptListData };
    this.closeDeptDrawer();
  };

  findDept(deptList, deptId) {
    let findedDept;
    const loop = (deptList, deptId) => {
      for (let item of deptList) {
        if (item.id == deptId) {
          findedDept = item;
          break;
        }

        if (item.children != null && item.children.length > 0) {
          loop(item.children, deptId);
        }
      }
    };
    loop(deptList, deptId);
    return findedDept;
  }

  /**
   * 删除机构响应函数
   * @param record 机构记录
   */
  async deleteDept(record) {
    let result = await deleteDept(record);
    if (result.code != 0) {
      message.error(result.msg);
      return;
    } else {
      message.success('删除部门成功！');
    }

    let findedParentDept = this.findDept(this.deptListData.dataSource, record.parentId);
    // 刷新机构列表
    let childrenDept = await loading(listDeptByParentId, this.deptListData)(record.parentId);
    if (findedParentDept != null) {
      findedParentDept.children = childrenDept;
      this.deptListData = { ...this.deptListData };
    } else {
      this.deptListData.dataSource = await loading(listDept, this.deptListData)(record.orgId);
    }


  };


  /**
   * 根据机构名称模糊查询机构列表
   * @param name 机构名称
   */
  async searchDept(orgId, name) {
    console.log(orgId, name);
    this.deptListData.dataSource = await loading(listDept, this.deptListData)(orgId, name);
    this.deptListData.expandedKeys = [];
  };
}

export default AppStore;
